package com.daxia.bumall.dao;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.daxia.bumall.common.DesignStatus;
import com.daxia.bumall.common.HouseAreaTag;
import com.daxia.bumall.common.Logs;
import com.daxia.bumall.daobase.GenericDAOHibernate;
import com.daxia.bumall.dto.DesignDTO;
import com.daxia.bumall.model.Design;
import com.daxia.bumall.model.HouseType;
import com.daxia.bumall.support.Page;

@Repository
public class DesignDAO extends GenericDAOHibernate<Design> {

    public List<Design> find(DesignDTO dto, Page page) {
        StringBuilder hql = new StringBuilder();   
        List<Object> paras = new ArrayList<Object>();
        hql.append("From Design n where 1 = 1 ");
        if (dto.getId() != null) {
            hql.append(" and n.id = ? ");
            paras.add(dto.getId());
        }
        if (StringUtils.isNotBlank(dto.getTitle())) {
            hql.append(" and n.title like ? ");
            paras.add("%" + dto.getTitle() + "%");
        }					
        if (dto.getHouseType() != null) {
        	if (dto.getHouseType().getId() != null) {
	            hql.append(" and n.houseType.id = ? ");
	            paras.add(dto.getHouseType().getId());
        	}
        }			
        if (dto.getHouseStyle() != null) {
        	if (dto.getHouseStyle().getId() != null) {
	            hql.append(" and n.houseStyle.id = ? ");
	            paras.add(dto.getHouseStyle().getId());
        	}
        }	
        if (dto.getCommunityHouseType() != null) {
        	if (dto.getCommunityHouseType().getId() != null) {
	            hql.append(" and n.communityHouseType.id = ? ");
	            paras.add(dto.getCommunityHouseType().getId());
        	}
        }
        if (StringUtils.isNotBlank(dto.getTheme())) {
            hql.append(" and n.theme like ? ");
            paras.add("%" + dto.getTheme() + "%");
        }
        if (dto.getUser() != null) {
        	if (dto.getUser().getSex() != null) {
        		hql.append(" and n.user.sex = ? ");
        		paras.add(dto.getUser().getSex());
        	}
        	if (dto.getUser().getId() != null) {
        		hql.append(" and n.user.id = ? ");
        		paras.add(dto.getUser().getId());
        	}
        }
        if (dto.getCompany() != null) {
        	hql.append(" and n.company.id = ? ");
        	paras.add(dto.getCompany().getId());
        }
        if (dto.getHouseAreaTag() != null) {
//        	hql.append(" and n.minArea >= ? and n.maxArea <= ? ");
        	hql.append(" and n.area >= ? and n.area <= ? ");
        	paras.add(HouseAreaTag.getByValue(dto.getHouseAreaTag()).getMinArea()+"");
        	paras.add(HouseAreaTag.getByValue(dto.getHouseAreaTag()).getMaxArea()+"");
        }
        if (dto.getDesigner() != null) {
        	if (dto.getDesigner().getId() != null) {
        		hql.append(" and n.designer.id = ? ");
        		paras.add(dto.getDesigner().getId());
        	}
        	if (dto.getDesigner().getCity() != null && dto.getDesigner().getCity().getId() != null) {
        		hql.append(" and n.designer.city.id = ? ");
        		paras.add(dto.getDesigner().getCity().getId());
        	}
        }
        if (dto.getType() != null) {
        	hql.append(" and n.type = ? ");
        	paras.add(dto.getType());
        }
        if (StringUtils.isNotBlank(dto.getPlanId())) {
        	hql.append(" and n.planId = ? ");
        	paras.add(dto.getPlanId());
        }
        if(null!=dto.getRecommend()&&dto.getRecommend()!=-1){
        	hql.append(" and n.recommend = ? ");
        	paras.add(dto.getRecommend());
        }

        if(null!=dto.getCommunity()){
        	if(null!=dto.getCommunity().getId()){
        		hql.append(" and n.community.id = ? ");
            	paras.add(dto.getCommunity().getId());
        	}
        	if(null!=dto.getCommunity().getProvince()){
        		hql.append(" and n.community.province.id = ? ");
            	paras.add(dto.getCommunity().getProvince().getId());
        	}
        	if(null!=dto.getCommunity().getCity()){
        		hql.append(" and n.community.city.id = ? ");
            	paras.add(dto.getCommunity().getCity().getId());
        	}
        	if(null!=dto.getCommunity().getDistrict()){
        		hql.append(" and n.community.district.id = ? ");
            	paras.add(dto.getCommunity().getDistrict().getId());
        	}
        }
        
        if (StringUtils.isNotBlank(dto.getArea())) {
        	hql.append(" and n.area = ? ");
        	paras.add(dto.getArea());
        }

        if (dto.isQueryHasVRUrl()) {
        	hql.append(" and n.VRURL is not null ");
        }
        
        if (!dto.isFromAdmin()) {
        	hql.append(" and n.status = ? ");
        	paras.add(DesignStatus.Pass.getValue());
        }
        if (dto.getStatus() != null) {
        	hql.append(" and n.status = ? ");
        	paras.add(dto.getStatus());
        }
        
        if (dto.getCityUserId() != null) {
        	hql.append(" and (n.designer.user.id is null and n.designer.user.city.id = (select city.id from User where id = ? )" // 个人
        			+ " or (n.designer.companyUser.id is not null and n.designer.companyUser.user.city = (select city.id from User where id = ? )))"); // 公司
        	paras.add(dto.getCityUserId());
        	paras.add(dto.getCityUserId());
        }
        if (dto.getCompanyUserId() != null) {
        	hql.append(" and n.designer.companyUser.id = ? ");
        	paras.add(dto.getCompanyUserId());
        }
        
        System.out.println("hql: " + hql);
        hql.append(" and (n.deleted is null or n.deleted = 0) ");
        
        //排序规则
        if(null!=dto.getOrderType()) {
	        switch(dto.getOrderType()){
		        case Price:
		        	hql.append(" order by n.price desc ");
		        	break;
		        case Hot :
		        	hql.append(" order by n.recommend desc ");
		        	break;
		        default :
		        	hql.append(" order by n.id desc ");	
	        }
        }
        else {
        	hql.append(" order by n.id desc ");
        }

        Logs.COMMON.debug(hql + ", " + Arrays.toString(paras.toArray()));
        System.out.println(hql);
        return super.find(hql.toString(), paras.toArray(), page);
    }
    
    public Design findOne(DesignDTO dto) {
		Page page = new Page();
		page.setPageSize(1);
		page.setPage(1);
		
		List<Design> list = this.find(dto, page);
	    return CollectionUtils.isEmpty(list) ? null : list.get(0);
    }

    public int count(Long user_id){
        return super.queryTotalCount("From Design  where user_id = ? ", new Object[]{user_id});
    }
    
    public int countByCompanyId(Long companyId) {
    	return super.countBySql("SELECT COUNT(*) FROM design WHERE company_id = ? ", new Object[]{companyId});
    }
    
    @Override
    public Long create(Design t) {
    	if (t.getRecommend() == null) {
    		t.setRecommend(0);
    	}
    	if (t.getStatus() == null) {
    		t.setStatus(DesignStatus.NotAudit.getValue());
    	}
    	return super.create(t);
    }

	public List<HouseType> findDistinctHouseTypesByCommunity(Long id) {
		String hql = "select distinct n.houseType from Design n where n.community.id = ?";
		return super.find2(hql, new Object[] {id}, null);
	}

	public List<String> findDistinctAreas(Long communityId, Long houseTypeId) {
		String hql = "select distinct n.area from Design n where n.community.id = ? and n.houseType.id = ? ";
		return super.find2(hql, new Object[] {communityId, houseTypeId}, null);
	}
    
}
